package com.nuoniu.sibanyun.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.SysCompany;
import com.nuoniu.sibanyun.entity.SysOffice;
import com.nuoniu.sibanyun.entity.dto.SysOfficeDto;
import com.nuoniu.sibanyun.entity.vo.OfficeTreeData;
import com.nuoniu.sibanyun.entity.vo.SelectVo;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 组织机构表 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-05-25
 */
@Component
@Mapper
public interface SysOfficeMapper extends BaseMapper<SysOffice> {

    @Select("select id, parent_id, office_name as name from sys_office where status = 0 and company_id = #{companyId}")
    List<OfficeTreeData> getOfficeTree(SysOfficeDto sysOfficeDto);

    /**
     * 删除子级
     * @author 小啊刚
     * @date 2020/6/12
     */
    @Delete("delete from sys_office where parent_id = #{id}")
    void deleteChildren(Long id);

    /**
     * 修改末级
     * @author 小啊刚
     * @date 2020/6/12
     */
    @Update("update sys_office set tree_leaf = #{leaf} where id = #{id}")
    void updateLevel(Integer id, Integer leaf);

    /**
     * 获取子级列表
     * @author 小啊刚
     * @date 2020/6/12
     */
    @Select("select * from sys_office where parent_id = #{id}")
    List<SysOffice> getOfficeByParentId(Long id);

    /**
     * 修改公司名称
     * @author 小啊刚
     * @date 2020-08-04
     */
    @Update("update sys_office set office_name = #{companyName}, full_name = #{companyName}, update_date = now() where company_id = #{id} and parent_id = 0")
    void updateCompanyName(SysCompany company);

    /**
     * 同步机构名称
     * @author 小啊刚
     * @date 2020-08-05
     */
    @Update("update ${tableName} set office_name = #{officeName} where office_id = #{id}")
    void updateOfficeName(String tableName, Long id, String officeName);

    @Select("select * from sys_office where company_id = #{id} and parent_id = 0")
    SysOffice getParentName(Long id);

    @Update("update sys_office set parent_ids=REPLACE(parent_ids, #{parentIds}, #{newParentIds}), tree_names=REPLACE(tree_names, #{treeNames}, #{newTreeNames}) " +
            "where  FIND_IN_SET(#{officeId}, parent_ids ) or id = #{officeId}")
    void updateChildrenParentIds(Long officeId, String parentIds, String treeNames, String newParentIds, String newTreeNames);

    /**
     * 查询已知 部门ID的所有下属部门ID
     * @param officeIds
     * @return
     */
    Set<String> findAllOfficeIds(@Param("officeIds") Collection<String> officeIds);

    List<OfficeTreeData> findAllOffices(@Param("officeIds") Collection<String> officeIds);

    List<OfficeTreeData> findAllOfficesByCompanyId(@Param("companyId") Integer companyId);

    @Select("SELECT * FROM sys_office WHERE company_id = #{companyId}")
    List<SysOffice> listByCompanyId(Integer companyId);

    @Select("select * from sys_office where office_code=#{officeCode} and company_id=#{companyId} and status!=1 limit 1")
    SysOffice getByCode(String officeCode, Integer companyId);

    /**
     * 根据名称获取本机构
     * @author 小啊刚
     * @date 2021-01-04
     */
    @Select("select * from sys_office where  company_id=#{companyId} and office_name=#{officeName} limit 1")
    SysOffice getOfficeName(String officeName, Long companyId);
}
